package cn.cihon.ubi.qorosauto.dao.file;

import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

import cn.cihon.ubi.qorosauto.biz.EconDataGenerator;
import cn.cihon.ubi.qorosauto.bo.TripData;

public class TripDataDao {
	
	BufferedWriter bw=null;
	boolean flag=false;
	public boolean storeTofile(TripData tripData)
	{
		String vin = tripData.getVin();
		String ecopoint = Double.toString(tripData.getEcoPoint());
		String safepoint = Double.toString(tripData.getSafePoint());
		Date date=new Date();
		DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time=format.format(date);
		
		Date time2 =  tripData.getRecordTimestamp();
		System.out.println(time2);
		String num = Integer.toString(tripData.getCount_acc());
		String FreWQAcc = Double.toString(tripData.getFreWQACC());
		String FreWQDcc = Double.toString(tripData.getFreWQDCC());
		String FreWQTcc = Double.toString(tripData.getFreWQTCC());
		String IdleTimeG120 = Double.toString(Double.valueOf(tripData.getDaisushijianchangdu() == null ? 0 : tripData.getDaisushijianchangdu()) / 1000);
		String ValidDataPoint = Double.toString(tripData.getYouxiaodeshujuliang());
		String tripLength = Double.toString(Double.valueOf(tripData.getLichengchangdu()));
		String AvgVehSpeed = Double.toString(Double.valueOf(tripData.getPingjunchesu()));
		String AvgEngSpeed = Double.toString(Double.valueOf(tripData.getPingjunfadongjisudu()));
		String StdAcc = Double.toString(Double.valueOf(tripData.getAccStd()));
		String tripConsum = Double.toString(Double.valueOf(tripData.getZongyouhao()));
		
		EconDataGenerator econ = new EconDataGenerator(tripData);
		
		String ExtraEcon = Double.toString(econ.getExtraEcon());
		String ExtraEconIdle = Double.toString(econ.getExtraEconIdle());
		String mark_Econ = Double.toString(econ.getMark_Econ());
		String mark_rpmPkm = Double.toString(econ.getMark_rpmPkm());
		String Mark_WQAcc = Double.toString(econ.getMark_WQAcc());
		String Mark_WQDcc = Double.toString(econ.getMark_WQDcc());
		String Mark_WQTcc = Double.toString(econ.getMark_WQTcc());
		String Mark_stdAcc = Double.toString(econ.getMark_stdAcc());
		String Mark_IdleTime = Double.toString(econ.getMark_IdleTime());
		
		
		//文件命名
		DateFormat format1=new SimpleDateFormat("yyyy-MM-dd");
		String time1=format1.format(date);
		String fileName=time1+vin;             //定义文件命名变量
		
		String str = time2+","+ecopoint+","+safepoint+","+FreWQAcc+","+FreWQDcc +","+FreWQTcc +","+IdleTimeG120 +","+ValidDataPoint+ ","+tripLength +","+AvgVehSpeed+ ","+AvgEngSpeed +","+StdAcc+","+ ExtraEcon+","+ ExtraEconIdle+","+ mark_Econ +","+mark_rpmPkm+","+ Mark_WQAcc+","+ Mark_WQDcc +","+Mark_WQTcc+","+ Mark_stdAcc +","+Mark_IdleTime;
		
		try {
			bw=new BufferedWriter(new FileWriter(new File("/home/yan/result/tripData/"+fileName),true)); //每次写追加到结尾
			//bw=new BufferedWriter(new FileWriter(new File("D:\\eclipse-java-luna-SR2-win32-x86_64\\eclipse\\workspace\\RealTimeComputing\\tripData\\"+fileName),true)); //每次写追加到结尾
			bw.write(tripData.toString());
			//bw.write(str);
			bw.newLine();
			bw.flush();
			bw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
}
